package top.tanglizhi.springboot3.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import top.tanglizhi.springboot3.util.IpUtils;

import java.util.Optional;

@Component
@Slf4j
public class AppListener implements ApplicationListener<ApplicationReadyEvent> {

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        log.info("应用启动完成");
        String ip = IpUtils.getLocalIp();
        Integer port = event.getApplicationContext().getEnvironment().getProperty("server.port", Integer.class);
        String contextPath = event.getApplicationContext().getEnvironment().getProperty("server.servlet.context-path");
        contextPath = Optional.ofNullable(contextPath).orElse("");
        log.info("===============================================================================");
        log.info("http://{}:{}/{}",ip, port, contextPath);
        log.info("http://{}:{}/{}","127.0.0.1", port, contextPath);
        log.info("===============================================================================");
    }
}
